package com.yzf.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.yzf.entity.Idea;
import com.yzf.util.db.DBHelp;
import com.yzf.util.db.RowMapper;

public class IdeaDao {
	
	private DBHelp<Idea> db = new DBHelp<Idea>();
	
	public void save(Idea idea){
		String sql = "insert into t_idea(id,title,content,createtime,employeeid,projectid) values (?,?,?,?,?,?)";
		db.executeSQL(sql, idea.getId(),idea.getTitle(),idea.getContent(),idea.getCreatetime(),idea.getEmployeeid(),idea.getProjectid());
	}
	
	public List<Idea> findByPid(String pid){
		String sql = "select id,title,content,createtime,employeeid,projectid from t_idea where projectid=? order by createtime desc";
		List<Idea> ideaList = db.executeQueryToList(sql, new IdeaRowMapper(), pid);
		return ideaList;
	}
	public Idea findByIid(String iid) {
		String sql = "select id,title,content,createtime,employeeid,projectid from t_idea where id=? order by createtime desc";
		return db.executeQueryToObject(sql, new IdeaRowMapper(), iid);
	}
	
	public void update(Idea idea ){
		String sql = "update  t_idea set title=?,content=?,createtime=? ,employeeid=?,projectid=? where id=?";
		db.executeSQL(sql, idea.getTitle(),idea.getContent(),idea.getCreatetime(),idea.getEmployeeid(),idea.getProjectid(),idea.getId());
	}
	public void delete(String id){
		String sql = "delete from t_idea where id=?";
		db.executeSQL(sql, id);
	}
	
	public class IdeaRowMapper implements RowMapper<Idea>{

		@Override
		public Idea mapRow(ResultSet rs) throws SQLException {
			
			Idea idea = new Idea();
			idea.setId(rs.getString("id"));
			idea.setContent(rs.getString("content"));
			idea.setCreatetime(rs.getString("createtime"));
			idea.setEmployeeid(rs.getString("employeeid"));
			idea.setProjectid(rs.getString("projectid"));
			idea.setTitle(rs.getString("title"));
			return idea;
		}
		
	}

	
	
	
}
